#include "test_album.h"

#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>

// this method can pass compilation on windows, but yields very odd result
void Process(const char *format) {
  // this will got nothing from args, because, there's no ... arg there
  // compile time error in linux, assertion error for windows
#if defined(SYS_IS_WINDOWS)
  va_list args;
  va_start(args, format);
  char buff[2048];
  vsnprintf_s(buff, sizeof(buff), _TRUNCATE, format, args);
  printf(buff);
  va_end(args);
#endif
}

void Print(const char *format, ...) {
  Process(format);
}

void Print2(const char *format, ...) {
  va_list args;
  va_start(args, format);
#if defined(SYS_IS_WINDOWS)
  char buff[2048];
  vsnprintf_s(buff, sizeof(buff), _TRUNCATE, format, args);
#else
  char *buff;
  if (vasprintf(&buff, format, args) == -1) {
    fprintf(stderr, "vasprintf() unable to allocate memory!\n");
    abort();
  }
#endif
  printf(buff);
  va_end(args);
#if !defined(SYS_IS_WINDOWs)
  free(buff);
#endif
}

namespace ct {
void ArgTest::Run(int argc, char *argv[]) {
  Print("hello, %s\n", "world");
  Print2("hello, %s\n", "Huang Peng");
}

} // ct
